package com.pactera.madp.cp.controller.oms.moment;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pactera.madp.common.core.util.R;
import com.pactera.madp.common.log.annotation.SysLog;
import com.pactera.madp.cp.api.constant.DeptConstants;
import com.pactera.madp.cp.api.dto.moment.MomentDetailsTendencyDTO;
import com.pactera.madp.cp.api.dto.moment.MomentDto;
import com.pactera.madp.cp.api.dto.moment.MomentTask;
import com.pactera.madp.cp.api.entity.WorkDeptEntity;
import com.pactera.madp.cp.api.feign.RemoteDeptService;
import com.pactera.madp.cp.api.vo.moment.MomentStaticsTendencyVO;
import com.pactera.madp.cp.api.vo.moment.MomentStatisticsDataVO;
import com.pactera.madp.cp.api.vo.moment.MomentVo;
import com.pactera.madp.cp.service.IWorkDeptService;
import com.pactera.madp.cp.service.MomentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.LinkedHashMap;
import java.util.List;

@RestController
@RequestMapping("/omsMoment")
@Api(value = "omsMoment", tags = "朋友圈")
public class MomentController {

    @Autowired
    private MomentService momentService;

    /**
     * 远程调用部门信息
     */
    @Autowired
    private RemoteDeptService remoteDeptService;

    @Autowired
    private IWorkDeptService workDeptService;

    @GetMapping("/query")
    @ApiOperation("朋友圈查询")
    public R query(Page page, @Validated MomentDto dto) {
        Page<MomentVo> voPage = momentService.selectByCondition((int)page.getCurrent(), (int)page.getSize(), dto);

        return R.ok(voPage);
    }

    @SysLog("朋友圈发表任务")
    @GetMapping("/task")
    @ApiModelProperty("朋友圈发表任务")
    public R<IPage<MomentTask>> task(MomentDto dto, Page page) {
        Page<MomentTask> task = momentService.task(dto, page);
        return R.ok(task);
    }

    @GetMapping("/getStaticsData/{deptId}")
    @ApiOperation(value = "华融湘江朋友圈统计基础信息")
    public R<MomentStatisticsDataVO> getStatisticsData(@PathVariable("deptId")Integer deptId) {
        R r = remoteDeptService.getWxDeptId(deptId);
        Integer wxDeptId = (Integer) r.getData();
        List<MomentVo> allMomentVos = momentService.getAllMomentVos();
        MomentStatisticsDataVO entity = momentService.getStatisticsDatas(deptId, wxDeptId,allMomentVos);
        return R.ok(entity);
    }

    @GetMapping("/getStatisticsDataPage/{deptId}")
    @ApiOperation(value = "华融湘江朋友圈统计分页查询")
    public R<IPage<MomentStatisticsDataVO>> getStatisticsDataPage(@PathVariable("deptId")Integer deptId, Page page, String name) {
        R r = remoteDeptService.getWxDeptId(deptId);
        Integer wxDeptId = (Integer) r.getData();
        WorkDeptEntity workDeptEntity = workDeptService.getOne(
                Wrappers.<WorkDeptEntity>lambdaQuery()
                        .select(WorkDeptEntity::getLevel)
                        .eq(WorkDeptEntity::getWxDepartmentId, wxDeptId)
        );
        List<MomentVo> allMomentVos = momentService.getAllMomentVos();
        Integer level = workDeptEntity.getLevel();
        if (level < DeptConstants.SUB_BRANCH_LEVEL) {
            LinkedHashMap pageResult = (LinkedHashMap) remoteDeptService.getChildrenPage(deptId, page).getData();
            Page<MomentStatisticsDataVO> vos = JSON.parseObject(JSON.toJSONString(pageResult), new TypeReference<Page<MomentStatisticsDataVO>>() {});
            vos.getRecords().forEach(s -> {
                MomentStatisticsDataVO entity1 = momentService.getStatisticsDatas(s.getId(), Integer.parseInt(s.getCode()),allMomentVos);
                s.setMomentTotalCount(entity1.getMomentTotalCount());
                s.setEnterpriseMomentCount(entity1.getEnterpriseMomentCount());
                s.setPersonalMomentCount(entity1.getPersonalMomentCount());
                s.setLikesCount(entity1.getLikesCount());
                s.setCommentsCount(entity1.getCommentsCount());
                s.setEmployeeCount(entity1.getEmployeeCount());
                s.setCode(s.getName());
                s.setName(entity1.getName());
            });
            return R.ok(vos);
        } else {
            LinkedHashMap pageResult = (LinkedHashMap) remoteDeptService.getUserPage(name, deptId, page).getData();
            Page<MomentStatisticsDataVO> vos = JSON.parseObject(JSON.toJSONString(pageResult), new TypeReference<Page<MomentStatisticsDataVO>>() {});
            vos.getRecords().forEach(s -> {
                MomentStatisticsDataVO entity1 = momentService.getStatisticsDatas(s.getCode(),allMomentVos);
                s.setMomentTotalCount(entity1.getMomentTotalCount());
                s.setEnterpriseMomentCount(entity1.getEnterpriseMomentCount());
                s.setPersonalMomentCount(entity1.getPersonalMomentCount());
                s.setLikesCount(entity1.getLikesCount());
                s.setCommentsCount(entity1.getCommentsCount());
                s.setEmployeeCount(entity1.getEmployeeCount());
                s.setCode(entity1.getCode());
                s.setName(entity1.getName());
            });
            return R.ok(vos);
        }
    }

    @GetMapping("/tendency")
    @ApiOperation(value = "华融湘江朋友圈统计趋势图")
    public R<List<MomentStaticsTendencyVO>> getContactDetailsTendency(MomentDetailsTendencyDTO dto){
        Integer type = dto.getType();
        switch (type) {
            case 1:
                return R.ok(momentService.getMomentTotalCount(dto));
            case 2:
                return R.ok(momentService.getEnterpriseMomentCount(dto));
            case 3:
                return R.ok(momentService.getPersonalMomentCount(dto));
            case 4:
                return R.ok(momentService.getLikesCount(dto));
            case 5:
                return R.ok(momentService.getCommentsCount(dto));
            case 6:
                return R.ok(momentService.getEmployeeCount(dto));
        }
        return R.failed("查询字段类型不在1-6内");
    }

}
